﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier.utils;
using System.Text.RegularExpressions;

namespace Cashier.pos
{
    public partial class IntegralForm : Form
    {
        private PayForm payForm;
        /// <summary>
        /// 可用积分
        /// </summary>
        private int integral;
        /// <summary>
        /// 可兑换最大金额
        /// </summary>
        private Decimal rechangeAmount;
        /// <summary>
        /// 支付单金额
        /// </summary>
        private Decimal orderAmount;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="payForm"></param>
        /// <param name="integral">积分</param>
        /// <param name="orderAmount">支付单金额</param>
        public IntegralForm(PayForm payForm, int integral, Decimal orderAmount)
        {
            InitializeComponent();
            this.payForm = payForm;
            this.integral = integral;
            this.orderAmount = orderAmount;
            this.rechangeAmount = ToolsUtil.IntegralExchangeMoney(integral);

            label_integral.Text = integral.ToString();
            label_exchangeMoney.Text = rechangeAmount.ToString("f2");
        }

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            if (keyData == Keys.Escape)
            {
                this.Close();
            }
            else if (keyData == Keys.Enter)
            {
                if (this.CountRechangeAmount())
                {
                    this.Close();
                }
            }
            return b;
        }

        /// <summary>
        /// 计算兑换金额
        /// </summary>
        protected bool CountRechangeAmount()
        {
            bool isSuccess = false;
            if (!String.IsNullOrEmpty(textbox_usedAmount.Text) && Regex.IsMatch(textbox_usedAmount.Text, RegexExpress.DECIMAL_TWO_BITS))
            {
                Decimal usedAmount = Decimal.Parse(textbox_usedAmount.Text.Trim());
                if (Decimal.Compare(usedAmount, rechangeAmount) <= 0)
                {
                    if (Decimal.Compare(usedAmount, orderAmount) <= 0)
                    {
                        payForm.UsedIntegralAmount(usedAmount);
                        isSuccess = true;
                    }
                    else
                    {
                        MessageBox.Show("输入金额超出支付金额！");
                    }
                }
                else
                {
                    MessageBox.Show("输入金额超出可兑换金额！");
                }
            }
            else
            {
                MessageBox.Show("输入金额格式不正确！");
            }
            return isSuccess;
        }
    }
}
